import { constantRoutes } from '@/router'
import { generatorDynamicRouter } from '@/router/generateRoutes'
import { treeToArray } from '@/utils/arrayToTree'

const state = {
  routes: [],
  addRoutes: [],
  menuList: []
}

const mutations = {
  SET_ROUTES: (state, routes) => {
    state.addRoutes = routes
    state.routes = constantRoutes.concat(routes)
  },
  SET_FLATTERNMenu: (state, routes) => {
    state.menuList = routes
  }
}
const baby = [{
  'menuId': 'babyManage',
  'menuName': '宝宝管理',
  'platformId': null,
  'parentId': 'baby',
  'orderNum': 0,
  'url': '/baby/manage',
  'target': 'menuBlank',
  'menuType': 'C',
  'visible': '0',
  'deleted': null,
  'perms': 'baby::manage',
  'icon': '#',
  'createBy': null,
  'createTime': '2001年2月19日',
  'updateBy': null,
  'updateTime': null,
  'remark': ''
},
{
  'menuId': 'babyMemorandum',
  'menuName': '备忘录',
  'platformId': null,
  'parentId': 'baby',
  'orderNum': 1,
  'url': '/baby/memorandum',
  'target': 'menuBlank',
  'menuType': 'C',
  'visible': '0',
  'deleted': null,
  'perms': 'baby::memorandum',
  'icon': '#',
  'createBy': null,
  'createTime': '2001年2月19日',
  'updateBy': null,
  'updateTime': null,
  'remark': ''
},
{
  'menuId': 'babyVaccine',
  'menuName': '疫苗记录',
  'platformId': null,
  'parentId': 'baby',
  'orderNum': 2,
  'url': '/baby/vaccine',
  'target': 'menuBlank',
  'menuType': 'C',
  'visible': '0',
  'deleted': null,
  'perms': 'baby::vaccine',
  'icon': '#',
  'createBy': null,
  'createTime': '2001年2月19日',
  'updateBy': null,
  'updateTime': null,
  'remark': ''
}
]
const actions = {
  generateRoutes ({ commit }, menuList) {
    menuList[1]['children'][0]['orderNum'] = 3
    menuList[1]['children'][1]['orderNum'] = 4
    menuList[1]['children'][2]['orderNum'] = 5
    menuList[1]['children'].push(...baby)
    commit('SET_FLATTERNMenu', treeToArray(menuList))
    return new Promise(resolve => {
      const { addRoutes, accessedRoutes } = generatorDynamicRouter(menuList.filter(x => x.url !== '/'))
      commit('SET_ROUTES', accessedRoutes)
      resolve(addRoutes)
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
